﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LightWeightORM.ORM.TableAccessor;
using SmartWarehouse.Common;
using SmartWarehouse.ORM.Entity;
using SmartWarehouse.Config;

namespace SmartWarehouse
{
    namespace BussinessLogic
    {
        namespace Basis
        {
            /// <summary>
            /// Summary description for ProductFacade
            /// </summary>
            public class ProductFacade
            {
                private TableAccessor<SwProduct> _productAccessor;

                public TableAccessor<SwProduct> ProductAccessor
                {
                    get { return _productAccessor; }
                    set { _productAccessor = value; }
                }

                public ProductFacade()
                {
                    this.ProductAccessor = AccessorFactory.ProductAccessor;
                }

                public SwMessage AddProduct(SwProduct product)
                {
                    SwMessage message = new SwMessage(SwMessageType.Ok, "成功添加");

                    //该产品是否存在
                    if (this.ProductAccessor.RecordCount(new Filter(SwProduct._Name, product.Name, ComparisonOperator.Equal)) != 0)
                    {
                        message.MessageText = "该产品已存在";
                        message.MessageType = SwMessageType.Error;
                        return message;
                    }

                    //不存在则添加
                    try
                    {
                        this.ProductAccessor.Insert(product);
                    }
                    catch (Exception ex)
                    {
                        message.MessageType = SwMessageType.Error;
                        message.MessageText = string.Format("添加产品失败，详细信息：{0}", ex.Message);
                    }

                    return message;
                }
            }
        }
    }
}